/****************************************************************************
 * FILE NAME: 		timer.h
 * 
 * PROJECT NAME: 	EGR330_Project
 * TEAM MEMBERS: 	Kevin Vermeer
 * 					Brandon Ross
 *					Tyler Selk
 *
 * 
 * CREATED: 		1 August 2011 (Brandon Ross)
 * LAST MODIFIED: 		
 * 
 * DESCRIPTION:		Handles functions necessary to operate timer peripherals
 ****************************************************************************/

/* HEADER GUARD */
#ifndef TIMER_H
#define TIMER_H

/********************************** TIMER0 **********************************/

/***********************************************
 * REGISTER:	TCCR0A
 * NAME:		Timer Control Register A
 * BIT FIELD:	Bit7 -> COM0A1
 *				Bit6 -> COM0A0
 *				Bit5 -> COM0B1
 *				Bit4 -> COM0B0
 *				Bit3 -> reserved
 *				Bit2 -> reserved
 *				Bit1 -> WGM01
 *				Bit0 -> WGM00
 ***********************************************/

/***********************************************
 * REGISTER:	TCCR0B
 * NAME:		Timer Control Register B
 * BIT FIELD:	Bit7 -> FOC0A
 *				Bit6 -> FOC0B
 *				Bit5 -> reserved
 *				Bit4 -> reserved
 *				Bit3 -> WGM02
 *				Bit2 -> CS02
 *				Bit1 -> CS01
 *				Bit0 -> CS00
 ***********************************************/
#define T0_MASK_CS0X 0x07
#define T0_POS_CS0X 0x00

#define T0_CLK_STOP 0x00
#define T0_CLK_CLKIO 0x01
#define T0_CLK_CLKIO_DF8 0x02
#define T0_CLK_CLKIO_DF64 0x03
#define T0_CLK_CLKIO_DF256 0x04
#define T0_CLK_CLKIO_DF1024 0x05
#define T0_CLK_EXTCLK_FALL 0x06
#define T0_CLK_EXTCLK_RISE 0x07

void T0_selectClockSource(uint8_t T0_CLK_SOURCE);

/***********************************************
 * REGISTER:	TCNT0
 * NAME:		Timer Register
 * BIT FIELD:	Bit7 -> TCNT07
 *				Bit6 -> TCNT06
 *				Bit5 -> TCNT05
 *				Bit4 -> TCNT04
 *				Bit3 -> TCNT03
 *				Bit2 -> TCNT02
 *				Bit1 -> TCNT01
 *				Bit0 -> TCNT00
 ***********************************************/
void T0_writeValue(uint8_t value);

uint8_t T0_readValue(void);

/***********************************************
 * REGISTER:	OCR0A
 * NAME:		Output Compare Register A
 * BIT FIELD:	Bit7 -> OCR0A7
 *				Bit6 -> OCR0A6
 *				Bit5 -> OCR0A5
 *				Bit4 -> OCR0A4
 *				Bit3 -> OCR0A3
 *				Bit2 -> OCR0A2
 *				Bit1 -> OCR0A1
 *				Bit0 -> OCR0A0
 ***********************************************/
void T0_writeCompareA(uint8_t value);

/***********************************************
 * REGISTER:	OCR0B
 * NAME:		Output Compare Register B
 * BIT FIELD:	Bit7 -> OCR0B7
 *				Bit6 -> OCR0B6
 *				Bit5 -> OCR0B5
 *				Bit4 -> OCR0B4
 *				Bit3 -> OCR0B3
 *				Bit2 -> OCR0B2
 *				Bit1 -> OCR0B1
 *				Bit0 -> OCR0B0
 ***********************************************/
void T0_writeCompareB(uint8_t value);

/***********************************************
 * REGISTER:	TIMSK0
 * NAME:		Timer Interrupt Mask Register
 * BIT FIELD:	Bit7 -> reserved
 *				Bit6 -> reserved
 *				Bit5 -> reserved
 *				Bit4 -> reserved
 *				Bit3 -> reserved
 *				Bit2 -> OCIE0B
 *				Bit1 -> OCIE0A
 *				Bit0 -> TOIE0
 ***********************************************/
#define T0_BIT_OCIE0B 0x04
#define T0_BIT_OCIE0A 0x02
#define T0_BIT_TOIE0 0x01

void T0_enableCompareBInterrupt(void);

void T0_enableCompareAInterrupt(void);

void T0_enableOverflowInterrupt(void);

void T0_disableCompareBInterrupt(void);

void T0_disableCompareAInterrupt(void);

void T0_disableOverflowInterrupt(void);

/***********************************************
 * REGISTER:	TIFR0
 * NAME:		Timer Interrupt Flag Register
 * BIT FIELD:	Bit7 -> reserved
 *				Bit6 -> reserved
 *				Bit5 -> reserved
 *				Bit4 -> reserved
 *				Bit3 -> reserved
 *				Bit2 -> OCF0B
 *				Bit1 -> OCF0A
 *				Bit0 -> TOV0
 ***********************************************/
#define T0_BIT_OCF0B 0x04
#define T0_BIT_OCF0A 0x02
#define T0_BIT_TOV0 0x01

void T0_resetCompareBFlag(void);

void T0_resetCompareAFlag(void);

void T0_resetOverflowFlag(void);

/********************************** TIMER1 **********************************/

/***********************************************
 * REGISTER:	TCCR1A
 * NAME:		Timer Control Register A
 * BIT FIELD:	Bit7 -> COM1A1
 *				Bit6 -> COM1A0
 *				Bit5 -> COM1B1
 *				Bit4 -> COM1B0
 *				Bit3 -> reserved
 *				Bit2 -> reserved
 *				Bit1 -> WGM11
 *				Bit0 -> WGM10
 ***********************************************/

/***********************************************
 * REGISTER:	TCCR1B
 * NAME:		Timer Control Register B
 * BIT FIELD:	Bit7 -> ICNC1
 *				Bit6 -> ICES1
 *				Bit5 -> reserved
 *				Bit4 -> WGM13
 *				Bit3 -> WGM12
 *				Bit2 -> CS12
 *				Bit1 -> CS11
 *				Bit0 -> CS10
 ***********************************************/

#define T1_MASK_CS1X 0x07
#define T1_POS_CS1X 0x00

#define T1_CLK_STOP 0x00
#define T1_CLK_CLKIO 0x01
#define T1_CLK_CLKIO_DF8 0x02
#define T1_CLK_CLKIO_DF64 0x03
#define T1_CLK_CLKIO_DF256 0x04
#define T1_CLK_CLKIO_DF1024 0x05
#define T1_CLK_EXTCLK_FALL 0x06
#define T1_CLK_EXTCLK_RISE 0x07

void T1_selectClockSource(uint8_t T1_CLK_SOURCE);

/***********************************************
 * REGISTER:	TCCR1C
 * NAME:		Timer Control Register B
 * BIT FIELD:	Bit7 -> FOC1A
 *				Bit6 -> FOC1B
 *				Bit5 -> reserved
 *				Bit4 -> reserved
 *				Bit3 -> reserved
 *				Bit2 -> reserved
 *				Bit1 -> reserved
 *				Bit0 -> reserved
 ***********************************************/

/***********************************************
 * REGISTER:	TCNT1H
 * NAME:		Timer Register (Upper bits)
 * BIT FIELD:	Bit7 -> TCNT115
 *				Bit6 -> TCNT114
 *				Bit5 -> TCNT113
 *				Bit4 -> TCNT112
 *				Bit3 -> TCNT111
 *				Bit2 -> TCNT110
 *				Bit1 -> TCNT19
 *				Bit0 -> TCNT18
 * REGISTER:	TCNT1L
 * NAME:		Timer Register (Lower bits)
 * BIT FIELD:	Bit7 -> TCNT17
 *				Bit6 -> TCNT16
 *				Bit5 -> TCNT15
 *				Bit4 -> TCNT14
 *				Bit3 -> TCNT13
 *				Bit2 -> TCNT12
 *				Bit1 -> TCNT11
 *				Bit0 -> TCNT10
 ***********************************************/
void T1_writeValue(uint16_t value);

uint16_t T1_readValue(void);

/***********************************************
 * REGISTER:	OCR1AH
 * NAME:		Output Compare Register A (high)
 * BIT FIELD:	Bit7 -> OCR0A15
 *				Bit6 -> OCR0A14
 *				Bit5 -> OCR0A13
 *				Bit4 -> OCR0A12
 *				Bit3 -> OCR0A11
 *				Bit2 -> OCR0A10
 *				Bit1 -> OCR0A9
 *				Bit0 -> OCR0A8
 * REGISTER:	OCR1AL
 * NAME:		Output Compare Register A (low)
 * BIT FIELD:	Bit7 -> OCR0A7
 *				Bit6 -> OCR0A6
 *				Bit5 -> OCR0A5
 *				Bit4 -> OCR0A4
 *				Bit3 -> OCR0A3
 *				Bit2 -> OCR0A2
 *				Bit1 -> OCR0A1
 *				Bit0 -> OCR0A0
 ***********************************************/
void T1_writeCompareA(uint16_t value);

/***********************************************
 * REGISTER:	OCR0BH
 * NAME:		Output Compare Register B (high)
 * BIT FIELD:	Bit7 -> OCR0B15
 *				Bit6 -> OCR0B14
 *				Bit5 -> OCR0B13
 *				Bit4 -> OCR0B12
 *				Bit3 -> OCR0B11
 *				Bit2 -> OCR0B10
 *				Bit1 -> OCR0B9
 *				Bit0 -> OCR0B8
 * REGISTER:	OCR1BL
 * NAME:		Output Compare Register B (low)
 * BIT FIELD:	Bit7 -> OCR0B7
 *				Bit6 -> OCR0B6
 *				Bit5 -> OCR0B5
 *				Bit4 -> OCR0B4
 *				Bit3 -> OCR0B3
 *				Bit2 -> OCR0B2
 *				Bit1 -> OCR0B1
 *				Bit0 -> OCR0B0
 ***********************************************/
void T1_writeCompareB(uint16_t value);

/***********************************************
 * REGISTER:	ICR1H
 * NAME:		Input Capture Register (high)
 * BIT FIELD:	Bit7 -> ICR115
 *				Bit6 -> ICR114
 *				Bit5 -> ICR113
 *				Bit4 -> ICR112
 *				Bit3 -> ICR111
 *				Bit2 -> ICR110
 *				Bit1 -> ICR19
 *				Bit0 -> ICR18
 * REGISTER:	ICR1L
 * NAME:		Input Capture Register (low)
 * BIT FIELD:	Bit7 -> ICR17
 *				Bit6 -> ICR16
 *				Bit5 -> ICR15
 *				Bit4 -> ICR14
 *				Bit3 -> ICR13
 *				Bit2 -> ICR12
 *				Bit1 -> ICR11
 *				Bit0 -> ICR10
 ***********************************************/

/***********************************************
 * REGISTER:	TIMSK1
 * NAME:		Timer Interrupt Mask Register
 * BIT FIELD:	Bit7 -> reserved
 *				Bit6 -> reserved
 *				Bit5 -> ICIE1
 *				Bit4 -> reserved
 *				Bit3 -> reserved
 *				Bit2 -> OCIE0B
 *				Bit1 -> OCIE0A
 *				Bit0 -> TOIE0
 ***********************************************/
#define T1_BIT_ICIE1 0x20
#define T1_BIT_OCIE1B 0x04
#define T1_BIT_OCIE1A 0x02
#define T1_BIT_TOIE1 0x01

void T1_enableCaptureInterrupt(void);

void T1_enableCompareBInterrupt(void);

void T1_enableCompareAInterrupt(void);

void T1_enableOverflowInterrupt(void);

void T1_disableCaptureInterrupt(void);

void T1_disableCompareBInterrupt(void);

void T1_disableCompareAInterrupt(void);

void T1_disableOverflowInterrupt(void);

/***********************************************
 * REGISTER:	TIFR1
 * NAME:		Timer Interrupt Flag Register
 * BIT FIELD:	Bit7 -> reserved
 *				Bit6 -> reserved
 *				Bit5 -> ICF1
 *				Bit4 -> reserved
 *				Bit3 -> reserved
 *				Bit2 -> OCF0B
 *				Bit1 -> OCF0A
 *				Bit0 -> TOV0
 ***********************************************/
#define T1_BIT_ICF1 0x20
#define T1_BIT_OCF1B 0x04
#define T1_BIT_OCF1A 0x02
#define T1_BIT_TOV1 0x01

void T1_resetCaptureFlag(void);

void T1_resetCompareBFlag(void);

void T1_resetCompareAFlag(void);

void T1_resetOverflowFlag(void);

/********************************** TIMER2 **********************************/

/***********************************************
 * REGISTER:	TCCR2A
 * NAME:		Timer Control Register A
 * BIT FIELD:	Bit7 -> COM2A1
 *				Bit6 -> COM2A0
 *				Bit5 -> COM2B1
 *				Bit4 -> COM2B0
 *				Bit3 -> reserved
 *				Bit2 -> reserved
 *				Bit1 -> WGM21
 *				Bit0 -> WGM20
 ***********************************************/

/***********************************************
 * REGISTER:	TCCR2B
 * NAME:		Timer Control Register B
 * BIT FIELD:	Bit7 -> FOC2A
 *				Bit6 -> FOC2B
 *				Bit5 -> reserved
 *				Bit4 -> reserved
 *				Bit3 -> WGM22
 *				Bit2 -> CS22
 *				Bit1 -> CS21
 *				Bit0 -> CS20
 ***********************************************/
#define T2_MASK_CS2X 0x07
#define T2_POS_CS2X 0x00

#define T2_CLK_STOP 0x00
#define T2_CLK_CLKIO 0x01
#define T2_CLK_CLKIO_DF8 0x02
#define T2_CLK_CLKIO_DF32 0x03
#define T2_CLK_CLKIO_DF64 0x04
#define T2_CLK_CLKIO_DF128 0x05
#define T2_CLK_CLKIO_DF256 0x06
#define T2_CLK_CLKIO_DF1024 0x07

void T2_selectClockSource(uint8_t T2_CLK_SOURCE);

/***********************************************
 * REGISTER:	TCNT2
 * NAME:		Timer Register
 * BIT FIELD:	Bit7 -> TCNT27
 *				Bit6 -> TCNT26
 *				Bit5 -> TCNT25
 *				Bit4 -> TCNT24
 *				Bit3 -> TCNT23
 *				Bit2 -> TCNT22
 *				Bit1 -> TCNT21
 *				Bit0 -> TCNT20
 ***********************************************/
void T2_writeValue(uint8_t value);

uint8_t T2_readValue(void);

/***********************************************
 * REGISTER:	OCR2A
 * NAME:		Output Compare Register A
 * BIT FIELD:	Bit7 -> OCR2A7
 *				Bit6 -> OCR2A6
 *				Bit5 -> OCR2A5
 *				Bit4 -> OCR2A4
 *				Bit3 -> OCR2A3
 *				Bit2 -> OCR2A2
 *				Bit1 -> OCR2A1
 *				Bit0 -> OCR2A0
 ***********************************************/
void T2_writeCompareA(uint8_t value);

/***********************************************
 * REGISTER:	OCR2B
 * NAME:		Output Compare Register B
 * BIT FIELD:	Bit7 -> OCR2B7
 *				Bit6 -> OCR2B6
 *				Bit5 -> OCR2B5
 *				Bit4 -> OCR2B4
 *				Bit3 -> OCR2B3
 *				Bit2 -> OCR2B2
 *				Bit1 -> OCR2B1
 *				Bit0 -> OCR2B0
 ***********************************************/
void T2_writeCompareB(uint8_t value);

/***********************************************
 * REGISTER:	TIMSK2
 * NAME:		Timer Interrupt Mask Register
 * BIT FIELD:	Bit7 -> reserved
 *				Bit6 -> reserved
 *				Bit5 -> reserved
 *				Bit4 -> reserved
 *				Bit3 -> reserved
 *				Bit2 -> OCIE2B
 *				Bit1 -> OCIE2A
 *				Bit0 -> TOIE2
 ***********************************************/
#define T2_BIT_OCIE2B 0x04
#define T2_BIT_OCIE2A 0x02
#define T2_BIT_TOIE2 0x01

void T2_enableCompareBInterrupt(void);

void T2_enableCompareAInterrupt(void);

void T2_enableOverflowInterrupt(void);

void T2_disableCompareBInterrupt(void);

void T2_disableCompareAInterrupt(void);

void T2_disableOverflowInterrupt(void);

/***********************************************
 * REGISTER:	TIFR2
 * NAME:		Timer Interrupt Flag Register
 * BIT FIELD:	Bit7 -> reserved
 *				Bit6 -> reserved
 *				Bit5 -> reserved
 *				Bit4 -> reserved
 *				Bit3 -> reserved
 *				Bit2 -> OCF2B
 *				Bit1 -> OCF2A
 *				Bit0 -> TOV2
 ***********************************************/
#define T2_BIT_OCF2B 0x04
#define T2_BIT_OCF2A 0x02
#define T2_BIT_TOV2 0x01

void T2_resetCompareBFlag(void);

void T2_resetCompareAFlag(void);

void T2_resetOverflowFlag(void);

/***********************************************
 * REGISTER:	ASSR
 * NAME:		Asynchronous Status Register
 * BIT FIELD:	Bit7 -> reserved
 *				Bit6 -> EXCLK
 *				Bit5 -> AS2
 *				Bit4 -> TCN2UB
 *				Bit3 -> OCR2AUB
 *				Bit2 -> OCR2BUB
 *				Bit1 -> TCR2AUB
 *				Bit0 -> TCR2BUB
 ***********************************************/

#endif
